package com.fsc.util.socket;

import com.alibaba.fastjson.JSON;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.fsc.entity.domain.ChatRecord;
import com.fsc.entity.domain.GroupChat;
import com.fsc.entity.dto.ChatGetGHistoryDTO;
import com.fsc.entity.dto.ChatGetPHistoryDTO;
import com.fsc.entity.dto.ChatGroupMsgDto;
import com.fsc.entity.vo.GChatRecordVO;
import com.fsc.entity.vo.PChatRecordVO;
import com.fsc.service.ChatRecordService;
import com.fsc.service.GroupChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class GroupEventHandler {
    @Autowired
    private SocketIOServer socketIOServer;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ChatRecordService chatRecordService;
    @Autowired
    private GroupChatService groupChatService;

    @OnEvent(value = EventConstant.GET_GROUP_HISTORY)
    public void getPersonHistory(SocketIOClient client, String data){
        ChatGetGHistoryDTO chatGetGHistoryDTO=JSON.parseObject(data, ChatGetGHistoryDTO.class);
        log.info("获取私聊记录：{}", chatGetGHistoryDTO);
        //获取15条群聊信息
        List<GChatRecordVO> pChats= chatRecordService.getGHistory(chatGetGHistoryDTO);
        //这里进行了toJson,记得转换成json字符串
        client.sendEvent(EventConstant.GET_GROUP_HISTORY,JSON.toJSONString(pChats));
    }

    @OnEvent(value = EventConstant.GROUP_MESSAGE)
    public void groupMessage(SocketIOClient client,String data){
        ChatGroupMsgDto chatGroupMsgDto= JSON.parseObject(data,ChatGroupMsgDto.class);
        System.out.println(data);
        Integer sendId= chatGroupMsgDto.getSendId();
        Integer groupId=chatGroupMsgDto.getGroupId();
        log.info("用户{}，发送群聊{}消息{}",sendId,groupId,data);
        //异步写入数据库
        writeMsgToDB(chatGroupMsgDto);
        //获取房间所有成员并发送消息
        //妈的自己也发送，操了。
        socketIOServer.getRoomOperations(String.valueOf(groupId)).sendEvent(EventConstant.GROUP_MESSAGE,data);
        //保存聊天记录
    }

    @OnEvent(value = EventConstant.JOIN_ALL)
    public void joinAll(SocketIOClient client,Integer userId,Integer[]groupIds){
        log.info("用户{}，加入群聊{}",userId, Arrays.toString(groupIds));
        for (Integer groupId:groupIds){
            client.joinRoom(String.valueOf(groupId));
        }
    }
    //没有receiveid的，因为是群聊
    @Async
    protected void writeMsgToDB(ChatGroupMsgDto chatGroupMsgDto){
        ChatRecord chatRecord=ChatRecord.builder()
                .sendId(chatGroupMsgDto.getSendId())
                .chatType(1)
                .messageType(chatGroupMsgDto.getMessageType())
                .message(chatGroupMsgDto.getMessage())
                .voice(chatGroupMsgDto.getVoice())
                .duration(chatGroupMsgDto.getDuration())
                .time(LocalDateTime.now())
                .build();
        chatRecordService.save(chatRecord);
        GroupChat groupChat=GroupChat.builder()
                .groupId(chatGroupMsgDto.getGroupId())
                .chatId(chatRecord.getId())
                .build();
        groupChatService.save(groupChat);
    }

}
